package ui;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import entity.ICalendar;

/**
 * A panel where the user is able to select the 
 * calendar they wish to add events to. 
 * 
 * @author Sean Young
 *
 */
public class CalendarPanel extends JPanel{
	
	private JComboBox<String> selectCalendar;
	private JPanel calendarPanel;
	private CalendarUI parent;
	
	/**
	 * Creates a CalendarPanel. The parent must be a reference
	 * to the core CalendarUI.
	 * @param parent The main panel of the CalendarUI
	 */
	public CalendarPanel(CalendarUI parent) {
		super();
		this.parent = parent;
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setBorder(BorderFactory.createLineBorder(Color.BLACK));
		
		calendarPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		calendarPanel.add(new JLabel("Selected Calendar"));
		String[] calendarArray = new String[parent.getCalendars().size()];
		for(int i = 0; i <parent.getCalendars().size(); i++) {
			calendarArray[i] = parent.getCalendars().get(i).getName();
		}
		
		if(calendarArray.length < 1) {
			String[] empty = {"Please Create a Calendar"};
			selectCalendar = new JComboBox<String>(empty);
			selectCalendar.setEnabled(false);
		} else {
			selectCalendar = new JComboBox<String>(calendarArray);
		}
		calendarPanel.add(selectCalendar);

		add(calendarPanel);
	}
	
	/**
	 * Refreshes the CalendarPanel. If new ICalendars were
	 * added then they will be added to the list.
	 */
	public void refresh() {
		if(parent.getCalendars().size() > 0) {
			selectCalendar.removeAllItems();
			for(ICalendar cal : parent.getCalendars()) {
				selectCalendar.addItem(cal.getName());
			}
			selectCalendar.setEnabled(true);
		}
	}
	/**
	 * Returns the index of the selected ICalendar in the 
	 * JComboBox of the panel.
	 * @return The index of the selected ICalendar
	 */
	public int getSelectedIndex() {
		return selectCalendar.getSelectedIndex();
	}
}
